package com.chat.client.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.chat.client.notification.NotificationView;
import com.chat.client.ui.R;

public class ChatService extends Service {
	
	private static final String TAG = ChatService.class.getSimpleName();
	Handler handler = new Handler(){
		
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == 0) {
				String str2 = (String)msg.obj;
				showToast(str2);
				showNotification(str2);
			}
		}
	};
	
	@Override
	public void onCreate() {
		super.onCreate();
		Log.d(TAG, "### onCreate invoke ###");
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		super.onStartCommand(intent, flags, startId);
		Log.d(TAG, "### onStartCommand invoke ###");
		return START_STICKY;
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		Toast.makeText(this, "서비스 종료!!!", Toast.LENGTH_LONG).show();
		Log.d(TAG, "### onDestroy invoke ###");
	}


	@Override
	public IBinder onBind(Intent intent) {
		Log.d(TAG, "### onBind invoke ###");
		return null;
	}
	
	/////////////////////////////////////////////
	// Method
	/////////////////////////////////////////////
	public void sendNotifiMessage(){
		
	}
	
	 /**
     * 토스트 알림 팝업
     */
    protected void showToast(String message) {
        // show the toast
    	TextView tv = new TextView(this);
    	tv.setText(message);
        Toast toast = new Toast(this);
        toast.setView(tv);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }
	
	/**
     * 알림 아이콘, 상태 표시줄 관련된 항목
     */
    protected void showNotification(String message) {
    	
        // 알림 관리자 서비스
        NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        // 테스트용 메시지
        CharSequence from = "조민수";

        // 알림을 선택하면 대신해서 작업을 수행해 주는 PendingInent 생성
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, NotificationView.class), 0);

        // ticker 텍스트 만들기
        String tickerText = getString(R.string.imcoming_message_ticker_text, message);

        // 알림 객체 생성
        Notification notif = new Notification(R.drawable.icon, tickerText,
                System.currentTimeMillis());

        // 알림 패널에 표시 할 정보 설정
        notif.setLatestEventInfo(this, from, message, contentIntent);

        // 100ms 지연 후 250m 진동 후 100ms에 대한 일시 정지 후 500ms 대한 진동
//        notif.vibrate = new long[] { 100, 250, 100, 500};

        // Note that we use R.layout.incoming_message_panel as the ID for
        // the notification.  It could be any integer you want, but we use
        // the convention of using a resource id for a string related to
        // the notification.  It will always be a unique number within your
        // application.
        nm.notify(R.string.imcoming_message_ticker_text, notif);
    }
    
	/////////////////////////////////////////////
	//	Inner class							
	/////////////////////////////////////////////
}
